package com.crealth;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;

import org.apache.log4j.Logger;


public class Config {
	private static Logger errorLog = Logger.getLogger("error_log");
	//private static Logger log = Logger.getLogger(Config.class);
    //private static Logger ctrl_log = Logger.getLogger("goip_vos_control");
    
	public static int CRMPort = 6000;
	public static int sipDispatchType = 0;  // Sip分派模式
	public static String sipServerIP;
	public static String sipAccount;
	public static String sipPwd;
	public static int sipFixed = 0;  // 是否 固定sip
	public static String incomingSipIP;
	public static String incomingSipAccount;
	public static String incomingSipPwd;
	public static int  incomingFixed = 0;  // 是否 固定sip
	public static int requestHandlerThread = 100;  //  消息处理的 线程数量
	public static int responseHandlerThread = 10;
	public static String memcachedSvrPort = "127.0.0.1:11211";
	public static boolean mobiPhoneEncrypt = false;
	
	public static int showSearchConsumerInfo = 1;  // 是否允许查询 拨打是 客户信息
	public static int recycleConsumer = 0;  // 是否回收超过  30天 没有回访的意向客户
	public static int recycleConsumerDay = 30;  // 间隔时间  超过这个天数 就回收
	
	public static int needLimitClientVersion = 0;  // 是否需要限制客户端版本
    
    public static void initConfig() throws Exception {
        Properties prop = new Properties();
        prop.load(ClassLoader.getSystemResourceAsStream("config.properties"));
                
        sipServerIP = prop.getProperty("sip_server_ip", "").trim();
        sipAccount = prop.getProperty("sip_account", "0").trim();
        sipPwd = prop.getProperty("sip_pwd", "").trim();
        incomingSipIP = prop.getProperty("incoming_sip_ip", "").trim();
        incomingSipAccount = prop.getProperty("incoming_sip_account", "0").trim();
        incomingSipPwd = prop.getProperty("incoming_sip_pwd", "").trim();
        String strtemp = prop.getProperty("request_handler_thread", "100").trim();
        requestHandlerThread = Integer.valueOf(strtemp);        
        strtemp = prop.getProperty("response_handler_thread", "10").trim();
        responseHandlerThread = Integer.valueOf(strtemp);
        strtemp = prop.getProperty("CRMPort", "6000").trim();
        CRMPort = Integer.valueOf(strtemp);
        strtemp = prop.getProperty("recycle_consumer", "0").trim();
        recycleConsumer = Integer.valueOf(strtemp);
        strtemp = prop.getProperty("recycle_consumer_day", "30").trim();
        recycleConsumerDay = Integer.valueOf(strtemp);
        strtemp = prop.getProperty("limit_client_version", "0").trim();
        needLimitClientVersion = Integer.valueOf(strtemp);
        
        memcachedSvrPort = prop.getProperty("memcached_svrport", "127.0.0.1:11211").trim();
        strtemp = prop.getProperty("mobi_encrypt", "0");
        int nTemp = Integer.valueOf(strtemp);
        if (nTemp == 0) {
        	mobiPhoneEncrypt = false;
        } else {
        	mobiPhoneEncrypt = true;
        }
        
        strtemp = prop.getProperty("sip_fixed", "0").trim();
        sipFixed = Integer.valueOf(strtemp);
        strtemp = prop.getProperty("incoming_fixed", "0").trim();
        incomingFixed = Integer.valueOf(strtemp);
    }
    
    public static void updateSipAccount() {
    	updateSipAccount(sipServerIP, sipAccount, sipPwd);
    	updateIncomingSipAccount(incomingSipIP, incomingSipAccount, incomingSipPwd);
    }
    
    public static void updateSipAccount(String strIP, String sipAct, String sipPwd) {
    	updateProperties("sip_server_ip", strIP);
    	updateProperties("sip_account", sipAct);
    	updateProperties("sip_pwd", sipPwd);
    }
    
    public static void updateIncomingSipAccount(String strIP, String sipAct, String sipPwd) {
    	updateProperties("incoming_sip_ip", strIP);
    	updateProperties("incoming_sip_account", sipAct);
    	updateProperties("incoming_sip_pwd", sipPwd);
    }
    
    
    /**  
     * 更新properties文件的键值对  
     * 如果该主键已经存在，更新该主键的值；  
     * 如果该主键不存在，则插件一对键值。  
     * @param keyname 键名  
     * @param keyvalue 键值  
     */   
     public static void updateProperties(String keyname,String keyvalue) {
    	 Properties props = new Properties();
         try {   
             props.load(ClassLoader.getSystemResourceAsStream("config.properties"));
             // 调用 Hashtable 的方法 put，使用 getProperty 方法提供并行性。   
             // 强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。   
             OutputStream fos = new FileOutputStream("config.properties");              
             props.setProperty(keyname, keyvalue);   
             // 以适合使用 load 方法加载到 Properties 表中的格式，   
             // 将此 Properties 表中的属性列表（键和元素对）写入输出流   
             props.store(fos, "Update '" + keyname + "' value");   
         } catch (IOException e) {   
        	 errorLog.error("属性文件更新错误");
         }   
     }
    
	
}

